﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;
using System.Text;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Plugin
{
    public class Live_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Plugin/Live_edit.cshtml";
        private string action = string.Empty;
        private int id = 0;
        public ILiveService _live { get; set; }
        public IUsersService _users { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("live_list", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            //取到操作类型
            string _action = Request.Query["action"];
            if (!string.IsNullOrEmpty(_action) && _action == CMSEnums.ActionEnum.Edit.ToString())
            {
                this.action = CMSEnums.ActionEnum.Edit.ToString();//修改类型
                int.TryParse(Request.Query["id"], out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                }
                if (!_live.Exists(this.id))
                {
                    JscriptMsg("内容不存在或已被删除！", "back");
                    filterContext.Result = result;
                }
            }
            else
            {
                this.id = -1;
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }
        // GET: /admin/Link_Edit/

        public ActionResult Index()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            else
            {
                Live model = new Live();
                ViewData["model"] = model;
            }
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave(string txtContent)
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("live_list", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    ViewBag.ClientScript = JscriptMsg("保存过程中发生错误啦！", string.Empty);
                    return result;
                }
                ViewBag.ClientScript = JscriptMsg("修改轮播成功！", "../live_list/index");
            }
            else //添加
            {
                ChkAdminLevel("live_list", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                ViewBag.ClientScript = JscriptMsg("添加轮播成功！", "../live_list/index");
            }
            return result;
        }

        private void ShowInfo(int _id)
        {
            var model = _live.GetModel(_id);
            if (model == null)
                model = new Live {title="标题",user_id=1 };
            ViewBag.username = _users.GetModel(model.user_id).user_name;
            ViewData["model"] = model;
        }

        private bool DoAdd()
        {
            bool result = false;
            Live model = new Live();
            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.user_id = _users.GetModel(CMSRequest.GetFormString("txtUsername", HttpContext)).id;
            model.sort_id = Utils.StrToInt(Request.Form["txtSortId"].FirstOrDefault().Trim(), 0);
            model.img_url = Request.Form["txtImgUrl"].FirstOrDefault().Trim();
            if (_live.Add(model))
            {
                _users.UpdateField(model.user_id, "is_live=1");
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加轮播：" + model.title);
                result = true;
            }
            return result;
        }

        private bool DoEdit(int _id)
        {
            bool result = false;
            var model = _live.GetModel(_id);
            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            int user_id= _users.GetModel(CMSRequest.GetFormString("txtUsername", HttpContext)).id;
            if(user_id!=model.user_id)//变
            {
                //取消老的
                _users.UpdateField(model.user_id, "is_live=0");
                //设置成新的
                _users.UpdateField(user_id, "is_live=1");
                model.user_id = user_id;
            }
            model.sort_id = Utils.StrToInt(Request.Form["txtSortId"].FirstOrDefault().Trim(), 0);
            model.img_url = Request.Form["txtImgUrl"].FirstOrDefault().Trim();
            if (_live.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改轮播：" + model.title);
                result = true;
            }
            return result;
        }
    }
}